package com.os.user.controller;

import com.os.user.service.UserService;
import com.os.user.bean.User;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 
 * @version v1.0.0
 * @description User 控制层
 * @date Created in 2018-08-26 22:34:04
 */
@RequestMapping(value = "/user")
@RestController
public class UserController{

    private static final Logger LOGGER = LoggerFactory.getLogger(UserController.class);

    @Autowired
    private UserService userService;

    /**
     * 查询全部
     * @param
     * @return
     */
    @RequestMapping(method = RequestMethod.GET)
    public List<User> list() throws Exception {
        return userService.list(null);
    }

    /**
     * 通过ID查找对象
     * @param userId
     * @return
     */
    @RequestMapping(value = "/{userId}", method = RequestMethod.GET)
    public User detail(@PathVariable Integer userId) throws Exception {
        return userService.getById(userId);
    }

    /**
     * 创建
     * @param user
     * @return
     */
    @RequestMapping(method = RequestMethod.POST)
    public User create(@RequestBody User user) throws Exception {
        return userService.save(user);
    }

    /**
     * 更新
     * @param user
     * @return
     */
    @RequestMapping(method = RequestMethod.PUT)
    public User update(@RequestBody User user) throws Exception {
        return userService.update(user);
    }

    /**
     * 删除
     * @param userId
     * @return
     */
    @RequestMapping(value = "/{userId}", method = RequestMethod.DELETE)
    public int delete(@PathVariable Integer userId) throws Exception {
        return userService.deleteById(userId);
    }

}
